package com.lottery.ruok.lottrey.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.adapter.ViewPagerTitleAdapter;
import com.lottery.ruok.lottrey.fragment.BankRemittanceFragment;
import com.lottery.ruok.lottrey.fragment.BaseFragment;
import com.lottery.ruok.lottrey.fragment.OtherRemittanceFragment;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.GetRechargeBankRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetRechargeBankResponse;
import com.lottery.ruok.lottrey.ui.ShapeIndicatorView;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
/*
* 人工通道
* */

public class TransferPayActivity extends BaseActivity {
    public ArrayList<GetRechargeBankResponse> getRechargeBankResponses;
    public int bankTransferIndex = 0;

    @BindView(R.id.custom_indicator)
    ShapeIndicatorView customIndicator;
    @BindView(R.id.tabs)
    TabLayout tabs;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transfer_pay);
        ButterKnife.bind(this);

        init();
    }

    private void init() {
        getList();
    }

    private void getList() {
        RetrofitBase.getRetrofit().create(GetRechargeBankRequest.class).getResult(UserInfo.getToken())
                .compose(RxHelper.<ArrayList<GetRechargeBankResponse>>handleResult())
                .subscribe(new RxSubscribe<ArrayList<GetRechargeBankResponse>>(this) {
                    @Override
                    protected void _onNext(ArrayList<GetRechargeBankResponse> g) {
                        getRechargeBankResponses = g;

                        ArrayList<BaseFragment> baseFragments = new ArrayList<>();
                        String[] titles = new String[getRechargeBankResponses.size()];
                        for(int i = 0; i < getRechargeBankResponses.size(); i++){
                            titles[i] = getRechargeBankResponses.get(i).getNames();
                            if(titles[i].equals("银行汇款")){
                                bankTransferIndex = i;
                                baseFragments.add(new BankRemittanceFragment());
                            } else {
                                OtherRemittanceFragment otherRemittanceFragment = new OtherRemittanceFragment();
                                Bundle bundle = new Bundle();
                                bundle.putInt("index", i);
                                otherRemittanceFragment.setArguments(bundle);
                                baseFragments.add(otherRemittanceFragment);
                            }
                        }
                        viewPager.setAdapter(new ViewPagerTitleAdapter(getSupportFragmentManager(), baseFragments, titles));
                        if (titles.length > 0) {
                            viewPager.setOffscreenPageLimit(titles.length);
                        }
                        tabs.setupWithViewPager(viewPager);

                        customIndicator.setupWithTabLayout(tabs);
                        customIndicator.setupWithViewPager(viewPager);
                    }

                    @Override
                    protected boolean showDialog() {
                        return false;
                    }
                });
    }
}
